import axios from "axios" 
axios.defaults.withCredentials = true;
import qs from "qs"
// axios.defaults.withCredentials = true

const service = axios.create({
  // baseURL:process.env.VUE_APP_BASE_API
})
service.interceptors.request.use(config => {
  // if (config.method === 'FILEPOST' || config.method === 'PROFILEPOST') {
  //     config.headers['Content-Type'] = 'multipart/form-data'
  //   }
  //   if (config.method === 'ApplicationPOST' || config.method === 'ApplicationPUT' || config.method === 'QUERYPOST' || config.method === 'QUERYPUT') {
  //     config.headers['Content-Type'] = 'application/json;charset=UTF-8'
  //   }
  return config

}, error => {
  console.log(error)
  Promise.reject(error)
})
// response拦截器
service.interceptors.response.use(
  response => {
    if (response.data.code == 200) {
      // loadingInstance.close()
      // 可以写本地存储在这里
      console.log(response.data,'fetch');
      if (response.data.data==="退出登录") {
        localStorage.setItem('manageUesr', '')
      localStorage.setItem('loginUser', '')
      }
      return response.data
    } else if (response.data.code == -15) {
      // Message.warning('登录信息已失效，请重新登录')
      // localStorage.setItem('hiseeAuthorities', '')
      // localStorage.setItem('hiseeGroupInfo', '')
      // localStorage.setItem('hiseeUserName', '')
      // localStorage.setItem('hiseeTrueName', '')
      // localStorage.setItem('redirect', '')
      router.push('/login')
      // loadingInstance.close()
    } else {
      // Message.error(response.data.message)
      return response.data
    }


  }, error => {
    // Message.error('遇到点小问题啦,不要紧张.请联系管理员解决哦!!' + error)

  }
)
export default (url = '', data = {}, method = '') => {
  // GET请求，约定格式 data: jsonStr
  if (method == 'GET') {
    return new Promise((resolve, reject) => {
      let params = data
      console.log(data);
      service.get(url, {
        params: params,
        paramsSerializer: function (params) {
          return qs.stringify(params, {
            // arrayFormat: 'brackets'
          })
        }
      })
        .then((res) => {
          if (res) {
            resolve(res)
          } else {
            resolve(res)
          }
        }).catch((err) => {
          reject(err)
        })
    })
  }

  // POST请求约定格式 data: jsonStr, formdata传json
  if (method == 'POST') {
    return new Promise((resolve, reject) => {
      console.log(JSON.stringify(data));
      service({
        method: 'POST',
        url: url,
        data:JSON.stringify(data),
        headers: {
          'Content-Type': '	application/json'
        }
      })
        .then((res) => {
          console.log(res);
          if (res) {
            resolve(res)
          } else {
            resolve(res)
          }
        }).catch((err) => {
          reject(err)
        })
    })
  }
  if (method == 'DELETE') {
    return new Promise((resolve, reject) => {
      console.log(JSON.stringify(data));
      service({
        method: 'DELETE',
        url: url,
        data:JSON.stringify(data),
        headers: {
          'Content-Type': '	application/json'
        }
      })
        .then((res) => {
          console.log(res);
          if (res) {
            resolve(res)
          } else {
            resolve(res)
          }
        }).catch((err) => {
          reject(err)
        })
    })
  }
 

  // PUT请求约定格式 data: jsonStr, formdata传json
  if (method == 'PUT') {
    return new Promise((resolve, reject) => {
      service({
        method: 'PUT',
        url: url,
        params: qs.stringify({
          'data': JSON.stringify(data)
        }),
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        }
      })
        .then((res) => {
          if (res) {
            resolve(res.data)
          } else {
            resolve(res)
          }
        }).catch((err) => {
          reject(err)
        })
    })
  }

  // DELETE约定格式 data: jsonStr, formdata传json
  if (method == 'DELETE') {
    return new Promise((resolve, reject) => {
      let params = {
        data: JSON.stringify(data)
      }
      service.delete(url, {
        params: params,
        paramsSerializer: function (params) {
          return qs.stringify(params, {
            arrayFormat: 'brackets'
          })
        }
      })
        .then((res) => {
          if (res) {
            resolve(res.data)
          } else {
            resolve(res)
          }
        }).catch((err) => {
          reject(err)
        })
    })
  }
}